# !/usr/bin/python
# -*- coding: utf-8 -*-

# from selenium import webdriver
# # 进入浏览器设置
# options = webdriver.ChromeOptions()
# # 设置中文
# options.add_argument('lang=zh_CN.UTF-8')
# # 更换头部
# options.add_argument('user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"')
# browser = webdriver.Chrome(chrome_options=options)
# url = "https://www.autohome.com.cn/advice/201810/923804.html"
# browser.get(url)

from selenium import webdriver
import xlwt

browser = webdriver.Chrome()
wb = xlwt.Workbook(encoding='utf-8')
sheet = wb.add_sheet('cookie')
list = ['sessionid', 'area', 'sessionip']
for i in range(len(list)):
     sheet.write(0, i, list[i])
row = 1
url = "https://www.autohome.com.cn/advice/201810/923804.html"

while True:
    browser.get(url)
    # # 通过js新打开一个窗口
    # newwindow = 'window.open("https://www.autohome.com.cn/advice/201810/923804.html");'
    # 删除原来的cookie
    browser.delete_all_cookies()
    # # 携带cookie打开
    # browser.add_cookie({'name':'ABC','value':'DEF'})
    # 通过js新打开一个窗口
    # browser.execute_script(newwindow)
    # input("查看效果")
    # browser.add_cookie({'name': '51zxw', 'value': 'www.51zxw.net'})
    # print(browser.get_cookies())
    for cookie in browser.get_cookies():
        if cookie['name'] == 'sessionid':
            print("%s --- %s" % (cookie['name'], cookie['value']))
            sheet.write(row, 0, cookie['value'])
        if cookie['name'] == 'area':
            print("%s --- %s" % (cookie['name'], cookie['value']))
            sheet.write(row, 1, cookie['value'])
        if cookie['name'] == 'sessionip':
            print("%s --- %s" % (cookie['name'], cookie['value']))
            sheet.write(row, 2, cookie['value'])
    row += 1
    wb.save('aa.xls')
    # browser.quit()
